﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EraNet.Core;
using EraNet.Core.Caching;
using EraNet.Core.Data;
using EraNet.Core.Events;
using EraNet.Core.Model.Enum;
using EraNet.Core.Model.SysManage;
using System.Linq.Expressions;

namespace EraNet.Services.SysManage
{
    public class SysConfigService : ISysConfigService
    {
        #region Constants
        private const string SYSCONFIG_BY_ID_KEY = "Era.SysConfig.id-{0}";
        private const string SYSCONFIG_PATTERN_KEY = "Era.SysConfig.";
        private const string SYSCONFIG_ALL = "Era.SysConfig.Mark35486987";
        #endregion

        #region Fields
        private readonly IRepository<SysConfigs> _sysconfigRepository;
        private readonly ICacheManager _cacheManager;
        private readonly IEventPublisher _eventPublisher;
        #endregion

        #region Ctor
        public SysConfigService(IRepository<SysConfigs> sysconfigRepository, ICacheManager cacheManager, IEventPublisher eventPublisher)
        {
            this._sysconfigRepository = sysconfigRepository;
            this._cacheManager = cacheManager;
            this._eventPublisher = eventPublisher;
        }
        #endregion

        #region Methods
        public SysConfigs GetConfigById(int CfgId)
        {
            if (CfgId == 0)
                return null;
            return GetAllConfig().Where(c => c.Id == CfgId).FirstOrDefault();
        }

        /// <summary>
        /// 插入新的配置节点 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int InsertConfig(SysConfigs model)
        {
            if (model == null)
                throw new ArgumentNullException("SysConfigs");

            int rs = _sysconfigRepository.Insert(model);

            _cacheManager.Remove(SYSCONFIG_ALL);

            //event notification
            _eventPublisher.EntityInserted(model);
            return rs;
        }

        /// <summary>
        /// 获取所有的配置节点 并缓存 
        /// </summary>
        /// <returns></returns>
        private IList<SysConfigs> GetAllConfig()
        {
            return _cacheManager.Get(SYSCONFIG_ALL, () =>
            {
                var p = _sysconfigRepository.Table.ToList();
                return p;
            });
        }

        /// <summary>
        /// 根据配置类型，获取数据
        /// </summary>
        /// <param name="DicType"></param>
        /// <returns></returns>
        public SysConfigs GetConfigByKey(string CfgKeys)
        {
            if (string.IsNullOrEmpty(CfgKeys))
                return null;
            return GetAllConfig().Where(c => c.CfgKeys == CfgKeys).FirstOrDefault();
        }
        /// <summary>
        /// 根据配置类型，获取数据
        /// </summary>
        /// <param name="DicType"></param>
        /// <returns></returns>
        public SysConfigs GetConfigByKey(string CfgKeys, ConfigFor ty)
        {
            if (string.IsNullOrEmpty(CfgKeys))
                return null;
            return GetAllConfig().Where(c => c.CfgKeys == CfgKeys && c.CfgType == (int)ty).FirstOrDefault();
        }


        /// <summary>
        /// 根据where条件获取实体
        /// </summary>
        /// <param name="whereLambda">where条件</param>
        /// <returns></returns>
        public SysConfigs GetEntityByFunc(Expression<Func<SysConfigs, bool>> whereLambda)
        {
            return _sysconfigRepository.Table.Where(whereLambda).FirstOrDefault();
        }

        /// <summary>
        /// 修改实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(SysConfigs entity)
        {
            return _sysconfigRepository.Update(entity);
        }

        #endregion
    }
}
